//Binh Danh sach Hoc sinh vao man hinh diem danh
package TruongNhaLib;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import www.truongnha.com.R;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class StudentAdapter extends ArrayAdapter<Model> {
	final Toast toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT);
	private final List<Model> list;
	private List<Model> original;
	private Dictionary<Integer, Integer> diff=new Hashtable<Integer, Integer>(); 
	private Boolean copy = true;//Only copy at the first time, change to false after that.
	private final Activity context;
	private int[] result;
	private Button btn_m;
	private Button btn_c;
	private Button btn_k;
	private Button btn_save;
	private Boolean submitPermission;
	public StudentAdapter(Activity context, List<Model> list, int[] result, Button btn_m, Button btn_c, Button btn_k, Button btn_save, Boolean submitPermission ) {
		super(context, R.layout.attend_item, list);
		this.context = context;
		this.list = list;	
		this.result = result;
		this.btn_m = btn_m; this.btn_c = btn_c; this.btn_k = btn_k;this.btn_save = btn_save;
		this.submitPermission = submitPermission;
		Log.e("adapter constructor", "result = "+ result[0]);
	}
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		diff = new Hashtable<Integer, Integer>();
		copy = true;
	};
	public void savedClick(){
		diff = new Hashtable<Integer, Integer>();
		copy = true;		
	}
	public List<Model> copyFrom(List<Model>B){
		int size = B.size();
		List<Model> A = new ArrayList<Model>(size);
		for(int i = 0;i<size;i++){
			Model aModel = B.get(i);
			A.add(new Model(aModel.getId(), aModel.getIndex(), aModel.getName(), aModel.getBirth(), aModel.getSex(), aModel.getStatus()) );
		}
		return A;
	}
	public void compare(int position){
		if( list.get(position).getStatus() == original.get(position).getStatus() ){
			if(diff.get(position)!=null)
				diff.remove(position);
			Log.e("compare at"+position, "Giong nhau" + list.get(position).getStatus() + original.get(position).getStatus());
		} else{
			diff.put(position, list.get(position).getStatus());
			Log.e("compare at"+position, diff.size() +"-Khac nhau" +list.get(position).getStatus() + original.get(position).getStatus());			
		}
		if(!diff.isEmpty()){
			btn_save.setBackgroundResource(R.drawable.selector_save_button_2);
			btn_save.setText("Lưu lại");
			btn_save.setEnabled(true);//changed -> enable submit
		}else{
			btn_save.setBackgroundResource(R.drawable.selector_save_button_1);
			btn_save.setText("Đã lưu");			
			btn_save.setEnabled(false);//nothing change
		}
	}
	static class ViewHolder {
		protected TextView name;
		protected TextView status;
		protected TriToggleButton triToggle;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(copy){
			this.original = copyFrom(list);
			Log.e("ori = ", this.original.size()+"");
			Log.e("ori = ", original.toString());			
			copy = false;
		}
		View view = null;
		if (convertView == null) {//Convert view equal null at the first time only
			Log.e("adapter", "convertView == null");
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.attend_item, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.status =(TextView) view.findViewById(R.id.std_status);
			viewHolder.status.setText(list.get(position).getSex()+"-"+ list.get(position).getBirth());
			viewHolder.triToggle = (TriToggleButton) view.findViewById(R.id.triToggleButton);
			view.setTag(viewHolder);
			viewHolder.triToggle.setTag(list.get(position));
			if(submitPermission){//Only mainteacher have permission to change the attendance
				viewHolder.triToggle.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						// TODO Auto-generated method stub					
						final Model element =(Model) viewHolder.triToggle.getTag();
						int triState = viewHolder.triToggle.getState();
						switch(triState)
						{
							case 0://Nghi hoc ko phep
							{	element.setStatus(0);
								result[0] = result[0]+ 1;
								btn_k.setText( result[0] + " - K");
								compare(element.getIndex()-1);
								toast.setText(element.getName()+ "\nNghỉ học không phép");
								toast.show();
								break;							
							}
							case 1://Nghi hoc co phep
							{	element.setStatus(1);
								result[0] = result[0]- 1;
								result[1] = result[1]+ 1;
								btn_k.setText(result[0]+" - K");
								btn_c.setText(result[1] + " - P");
								compare(element.getIndex()-1);
								toast.setText(element.getName()+ "\nNghỉ học có phép");
								toast.show();
								break;							
							}
							case 2://Di hoc muon
							{	element.setStatus(2);
								result[1] = result[1]- 1;
								result[2] = result[2]+ 1;
								btn_c.setText(result[1]+" - P");
								btn_m.setText(result[2] + " - M");
								compare(element.getIndex()-1);
								toast.setText(element.getName()+ "\n Đi học muộn");
								toast.show();
								break;							
							}
							case 3://Di hoc
							{	element.setStatus(3);
								result[2] = result[2]- 1;
								btn_m.setText(result[2]+" - M");
								compare(element.getIndex()-1);
								toast.setText(element.getName()+ "\n Đi học");
								toast.show();	
								break;							
							}
							default:break;
						}
					}
				});//End SetOnclickListenner
			} else{//Clickable = false for other teacher
				viewHolder.triToggle.setBackgroundDrawable(null);
				viewHolder.triToggle.setClickable(false);
			}
			viewHolder.name = (TextView) view.findViewById(R.id.std_name);
			viewHolder.name.setText(list.get(position).getIndex()+"- "+list.get(position).getName());
		} else {
			Log.e("adapter", "use convertview ");
			view = convertView;
			((ViewHolder) view.getTag()).triToggle.setTag(list.get(position));
		}//end if.
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.name.setText(String.format("%2d. %s", list.get(position).getIndex(), list.get(position).getName()) );
		holder.triToggle.setState(list.get(position).getStatus());
		return view;
	}
	
}

	